<?php include('../includes/header.php');?>
<?php include('../includes/mysqli_connect.php');?>
<?php include('../includes/function.php');?>
<?php include('../includes/slide-admin.php'); 
// Kiem tra xem nguoi dung co duoc vao trang admin hay khong?
 
        
?>
<div id="content">
<h2>Manage Categories</h2>
<table>
	<thead>
		<tr>
			<th><a href="view_categories.php?sort=cat">Page Name</a></th>
			<th><a href="view_categories.php?sort=on">Posted on</th>
			<th><a href="view_categories.php?sort=by">Posted by</th>
            <th>Edit</th>
            <th>Delete</th>
		</tr>
	</thead>
	<tbody>
    <?php
        // Sap xep theo thu tu cua table head
            if(isset($_GET['sort'])) {
                switch ($_GET['sort']) {
                    case 'cat':
                    $order_by = 'page_name';
                    break;
                    
                    case 'on':
                    $order_by = 'date';
                    break;
                    
                    case 'by':
                    $order_by = 'name';
                    break;
                    
                    default:
                    $order_by = 'date';
                    break;
                } // End Switch
            } else {
                $order_by = 'position';
            }
        // truy xuat csdl de hien thi categories
        $q = "SELECT p.page_id, p.page_name,DATE_FORMAT(p.post_on,'%b %d %Y') AS date, CONCAT_WS(' ', first_name, last_name) AS name,p.content";
        $q .= " FROM pages AS p ";
        $q .= " JOIN users AS u ";
        $q .= " USING(user_id) ";
        $q .= " ORDER BY {$order_by} ASC";
        $r = mysqli_query($dbc, $q);
            confirm_query($r, $q);
        if(mysqli_num_rows($r)>0){
            while($pages = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
            echo "
            <tr>
                <td>{$pages['page_name']}</td>
                <td>{$pages['date']}</td>
                <td>{$pages['name']}</td>
                <td><a class='edit' href='edit_page.php?pid={$pages['page_id']}'>Edit</a></td>
                <td><a class='delete' href='delete_page.php?pid={$pages['page_id']}' >Delete</a></td>
            </tr>            
            ";
             }  
        }
        else {
            //neu khong co page de hien thi bao loi hoac noi nguoi dung tao page
            $messages="<p class='warning'>There no have any page . Please add page</p>";
        }
    ?>
        
	</tbody>
</table>
</div><!--end content-->

<?php include('../includes/footer.php'); ?>